﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAtletIS.Models;

namespace MvcAtletIS.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class ClubsController : Controller
    {
        private atletisEntities db = new atletisEntities();

        // GET: /Clubs/
        public ViewResult Index(string groupId)
        {
            var clubs = db.Clubs.OrderBy(c => c.City).Include("ClubsGroups");
            ViewBag.ClubsGroups = new SelectList(db.ClubsGroups.Where(dl => dl.Active == true).OrderBy(dl => dl.Name).ToList(), "ID", "Name");
            
            if (!string.IsNullOrEmpty(groupId))
            {
                clubs = clubs.Where(c => c.ClubsGroupsID == groupId);
            }

            return View(clubs.ToList());
        }

        // GET: /Clubs/Details/5
        public ViewResult Details(string id)
        {
            Clubs clubs = db.Clubs.Single(c => c.ID == id);
            return View(clubs);
        }

        // GET: /Clubs/Create
        public ActionResult Create()
        {
            ViewBag.ClubsGroupsID = new SelectList(db.ClubsGroups, "ID", "Name");
            return View();
        } 

        // POST: /Clubs/Create
        [HttpPost]
        public ActionResult Create(Clubs clubs)
        {
            clubs.Modified = DateTime.Now;
            clubs.Sync = true;

            if (ModelState.IsValid)
            {
                db.Clubs.AddObject(clubs);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.ClubsGroupsID = new SelectList(db.ClubsGroups, "ID", "Name", clubs.ClubsGroupsID);
            return View(clubs);
        }
        
        // GET: /Clubs/Edit/5
        public ActionResult Edit(string id)
        {
            Clubs clubs = db.Clubs.Single(c => c.ID == id);
            ViewBag.ClubsGroupsID = new SelectList(db.ClubsGroups, "ID", "Name", clubs.ClubsGroupsID);
            return View(clubs);
        }

        // POST: /Clubs/Edit/5
        [HttpPost]
        public ActionResult Edit(Clubs clubs)
        {
            clubs.Modified = DateTime.Now;
            clubs.Sync = true;
            
            if (ModelState.IsValid)
            {
                db.Clubs.Attach(clubs);
                db.ObjectStateManager.ChangeObjectState(clubs, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ClubsGroupsID = new SelectList(db.ClubsGroups, "ID", "Name", clubs.ClubsGroupsID);
            return View(clubs);
        }

        // GET: /Clubs/Delete/5
        public ActionResult Delete(string id)
        {
            Clubs clubs = db.Clubs.Single(c => c.ID == id);
            return View(clubs);
        }

        // POST: /Clubs/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {            
            Clubs clubs = db.Clubs.Single(c => c.ID == id);
            db.Clubs.DeleteObject(clubs);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}